package com.example.administrator.xiaoxiang.fragment.loginfragment;


import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.administrator.xiaoxiang.R;
import com.example.administrator.xiaoxiang.activity.MainActivity;
import com.example.administrator.xiaoxiang.fragment.PersonalFragment;

/**
 * A simple {@link Fragment} subclass.
 */
public class StandLoginFragment extends Fragment implements View.OnClickListener{
    private ImageView imageView;
    private EditText editText;
    private boolean flag = false;
    private EditText phoneNum;
    private Button button;

    public StandLoginFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_stand_login, container, false);
        init(view);
        return view;
    }

    private void init(View view) {
        imageView = (ImageView) view.findViewById(R.id.hide);
        editText = (EditText) view.findViewById(R.id.login_input_code_et);
        phoneNum = (EditText) view.findViewById(R.id.login_input_phone_et);
        button = (Button) view.findViewById(R.id.login_commit_btn);
        button.setOnClickListener(this);
        phoneNum.setText(MainActivity.phoneNum);
        imageView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.hide:
                flag = !flag;
                if (flag) {
                    editText.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                    editText.setSelection(editText.getText().toString().length());
                    imageView.setImageBitmap(BitmapFactory.decodeResource(getResources(),R.mipmap.show));
                }else{
                    editText.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD|InputType.TYPE_CLASS_TEXT);
                    editText.setSelection(editText.getText().toString().length());
                    imageView.setImageBitmap(BitmapFactory.decodeResource(getResources(),R.mipmap.hide));
                }
                break;
            case R.id.login_commit_btn:
                if ((editText.getText().toString().equals(MainActivity.password)
                        && editText.getText().toString().length()>3&&phoneNum.getText().toString().length()==11)
                        ||(phoneNum.getText().toString().equals("13553065418")&&editText.getText().toString().equals("1234"))//测试用
                    ) {
                    Toast.makeText(getContext(), "登录成功！", Toast.LENGTH_SHORT).show();
                    MainActivity activity = (MainActivity) getActivity();
                    activity.showFragment(PersonalFragment.TAG,PersonalFragment.class);
                    activity.isLongin = true;
                }else{
                    Toast.makeText(getContext(), "账号或密码错误", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
}
